import {defineConfig, loadEnv} from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import {ElementPlusResolver} from 'unplugin-vue-components/resolvers'
import vueJsx from "@vitejs/plugin-vue-jsx";
import {visualizer} from 'rollup-plugin-visualizer';
import {createHtmlPlugin} from "vite-plugin-html"
import * as path from "path";

const getTarget = (mode, target) => {
    return loadEnv(mode, process.cwd())[target];
};

// https://vitejs.dev/config/
export default defineConfig(({mode}) => {
    const env = loadEnv(mode, process.cwd());
    const {VITE_APP_BASE_API, NODE_ENV} = env;
    console.log(NODE_ENV);
    return {
        plugins: [
            vue(),
            //自动引入element-ui的组件
            AutoImport({
                resolvers: [ElementPlusResolver()],
            }),
            Components({
                resolvers: [ElementPlusResolver()],
            }),
            createHtmlPlugin({
                inject: {
                    data: {
                        title: getTarget(mode, "VITE_APP_TITLE"),
                    },
                },
            }),
            vueJsx()
            // 将 visualizer 插件放到最后的位置
            // visualizer({
            //     gzipSize: true,
            //     brotliSize: true,
            //     emitFile: false,
            //     open: true //如果存在本地服务端口，将在打包后自动展示
            // })
        ],
        css: {
            preprocessorOptions: {
                scss: {
                    javascriptEnabled: true,
                    additionalData: '@import "./src/styles/variable.scss";',
                },
            },
        },
        resolve: {
            alias: {
                "@": path.resolve(__dirname, './src') // 相对路径别名配置，使用 @ 代替 src
            }
        },
        server: {
            host: 'localhost',
            port: 4713,
            strictPort: false, // 设为 false 时，若端口已被占用则会尝试下一个可用端口,而不是直接退出
            proxy: {
                '/api': {
                    target: VITE_APP_BASE_API,
                    changeOrigin: true,
                    rewrite: (path) => {
                        return path.replace(/^\/api/, '')
                    }
                }
            },
            hmr: true
        },
        // esbuild:{
        //     drop: ['console','debugger'], // 删除 所有的console 和 debugger
        // },
        // base: './',
        build: {
            // assetsPublicPath: './',
            rollupOptions: {
                // external: [
                //     'vue',
                //     'vue-router',
                //     'element-plus',
                //     'pinia',
                // ],
                output: {
                    chunkFileNames: 'static/js/[name]-[hash].js',
                    entryFileNames: 'static/js/[name]-[hash].js',
                    assetFileNames: 'static/[ext]/[name]-[hash].[ext]',
                    manualChunks(id, {getModuleInfo}) {
                        // 打包依赖
                        if (id.includes('node_modules')) {
                            return 'vendor';
                        }

                        const reg = /(.*)\/src\/components\/(.*)/
                        if (reg.test(id)) {
                            const importersLen = getModuleInfo(id).importers.length;
                            // 被多处引用
                            if (importersLen > 1) {
                                return 'common';
                            }
                        }
                    }
                }
            },
        }
    }

})
